using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Binary0 : Riddle
{
    private List<BinaryPart> normalList = new List<BinaryPart>();
    // private BinaryPart result;

    private string leftStr = "";
    private int right;
    
    private void Start()
    {
        riddleCtr = GetComponentInParent<RiddleController>();
        foreach (Transform child in transform)
        {
            BinaryPart binaryPart = child.GetComponent<BinaryPart>();
            if (binaryPart)
            {
                if (binaryPart.BinaryRole == BinaryRole.Normal)
                    normalList.Add(binaryPart);
                else if (binaryPart.BinaryRole == BinaryRole.Result)
                {
                    // result = binaryPart;
                    right = Int32.Parse(binaryPart.GetText());
                }
            }
        }
        CheckAnswer();
    }

    public override void CheckAnswer()
    {
        leftStr = "";
        foreach (var binaryPart in normalList)
        {
            leftStr += binaryPart.GetText();
        }

        RiddleState = right == Convert.ToInt32(leftStr,2) ? RiddleState.Solved : RiddleState.UnSolved;
        riddleCtr.CheckRiddles();
        if (RiddleState == RiddleState.Solved)
            foreach (var binaryPart in normalList)
            {
                binaryPart.canEdit = false;
            }
    }
}